package com.innovation.chengzhangriji.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.ChZhRJApplication;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.AllBabyResponse;
import com.innovation.chengzhangriji.http.Auth;
import com.innovation.chengzhangriji.http.BabyResponse;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.event.BaseEvent;
import com.orhanobut.logger.Logger;

import de.greenrobot.event.EventBus;

public class GetAllBabyInfoService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LoginInfo login = Config.getLoginInfo(this);
        if (login == null) {
            return super.onStartCommand(intent, flags, startId);
        }
        String phone = login.getPhone();
        final String userId = login.getUserID();
        String token = login.getToken().getAccessToken();
        Client.requestAllBaby(this, phone, token, new ResponseHandler() {

            @Override
            public void onInnovationSuccess(JsonElement value) {
                Logger.d("有新消息,获取所有宝宝信息成功");
                AllBabyResponse response = get(value.toString(), AllBabyResponse.class);
                if (response == null) {
                    return;
                }
                int length = response.getData().length;
                int[] childIDArray = new int[length];
                Auth[] auth = new Auth[length];
                for (int i = 0; i < length; i++) {
                    childIDArray[i] = response.getData()[i].getId();
                    auth[i] = response.getData()[i].getAuth();
                }
                insertOrReplaceInTxBabyAndParent(userId, auth, childIDArray);
                insertOrReplaceInTxBaby(response.getData());
                sendBroadcast();
            }

        }, null);

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void insertOrReplaceInTxBabyAndParent(String id, Auth[] auth, int... babyIDArray) {
        DatabaseManager.getInstance(this).insertOrReplaceInTxBabyAndParent(id, auth, babyIDArray);
    }

    private void insertOrReplaceInTxBaby(BabyResponse... babyResponses) {
        DatabaseManager.getInstance(this).insertOrReplaceInTxBaby(babyResponses);
    }

    private void sendBroadcast() {
//        Intent intent = new Intent("change");
//        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        // 获取成功
        BaseEvent event = new BaseEvent();
        event.setType(BaseEvent.TYPE_SYNC_BABY_INFO);
        EventBus.getDefault().post(event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        ChZhRJApplication.getRefWatcher(this).watch(this);
    }
}
